{% extends 'admin.html' %}

{% block css %}
{% endblock %}

{% block left %}
    <ul class="layui-nav layui-nav-tree" lay-filter="test">
        <li class="layui-nav-item layui-nav-itemed ">
            <a class="" href="{% url 'admin_home' %}">书架</a>

        </li>
        <li class="layui-nav-item layui-nav-itemed">
            <a href="javascript:;">我的权限</a>
            <dl class="layui-nav-child ">
                {% if request.admin == 'true' %}
                    <dd><a href="{% url 'admin_toAdmin' %}">管理员管理</a></dd>
                {% endif %}
                <dd class="layui-this"><a href="{% url 'manage_user' %}">用户管理</a></dd>
            </dl>
        </li>
        <li class="layui-nav-item">
            <a href="javascript:;">个人中心</a>
            <dl class="layui-nav-child">
                <dd><a href="{% url 'admin_detail' %}">账号详情</a></dd>
            </dl>
        </li>
    </ul>
{% endblock %}

{% block body %}
    <table class="layui-table">
        {% if status == 'user' %}
            <span class="layui-breadcrumb">
                <a href="{% url 'admin_home' %}">首页</a>

                <a><cite>我的权限</cite></a>
                <a href="{% url 'manage_user' %}">用户管理</a>
            </span>
            <hr/>
            <h1 style="text-align: center">用户列表</h1>
            <hr/>
            <tr>
                <th>用户名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>操作</th>
            </tr>
            {% for user in user_list %}
                <tr>
                    <td>{{ user.username }}</td>
                    {% if user.sex is none %}
                        <td>待完善</td>
                    {% else %}
                        {% if user.sex == 1 %}
                            <td>男</td>
                        {% else %}
                            <td>女</td>
                        {% endif %}
                    {% endif %}
                    {% if user.age is none %}
                        <td>待完善</td>
                    {% else %}
                        <td>{{ user.age }}</td>
                    {% endif %}
                    <td>
                        <a href="{% url 'show_userRecord' %}?userId={{ user.id }}" class="layui-btn layui-btn-normal">借阅信息</a>
                        <button class="layui-btn layui-btn-danger" onclick="del({{ user.id }})">删除用户</button>
                    </td>
                </tr>
            {% endfor %}
        {% else %}
            <span class="layui-breadcrumb">
                <a href="{% url 'admin_home' %}">首页</a>

                <a><cite>我的权限</cite></a>
                <a href="{% url 'manage_user' %}">用户管理</a>
                <a><cite>借阅详情</cite></a>
            </span>
            <hr/>
            <h1>{{ userObj.username }} 借阅详情</h1>
            <hr/>
            <tr>
                <th>图书</th>
                <th>借书日期</th>
                <th>应还日期</th>
                <th>是否归还</th>
                <th>归还日期</th>
                <th>是否超时</th>
            </tr>

            {% for record in record_list %}
                <tr>
                    <td>{{ record.bookId.book_name }}</td>
                    <td>{{ record.borrowing_time }}</td>
                    <td>{{ record.due_date }}</td>
                    {% if record.status == 1 %}
                        <td>已归还</td>
                    {% else %}
                        <td style="color: red">未归还</td>
                    {% endif %}
                    {% if record.return_time is none %}
                        <td>还未归还图书</td>
                    {% else %}
                        <td>{{ record.return_time }}</td>
                    {% endif %}
                    {% if record.flag == 0 %}
                        <td>按时归还</td>
                    {% elif  record.flag  == 1 %}
                        <td style="color: red">超时归还</td>
                    {% else %}
                        <td>暂未归还</td>
                    {% endif %}

                </tr>
            {% endfor %}
        {% endif %}
    </table>
{% endblock %}

{% block js %}
    <script>

        function del(userId) {
            layer.open({
                content: '确认删除？',
                btn: ['确认', '取消'],
                yes: function (index, layero) {
                    $.ajax({
                        url: '{% url 'del_user' %}',
                        type: 'POST',
                        data: {'userId': userId},
                        dataType: 'JSON',
                        success: function (res) {
                            if (res.status) {
                                layer.open({
                                        content: '删除成功',
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            location.href = '{% url 'manage_user' %}'
                                        }
                                    }
                                )
                            } else {
                                layer.open({
                                    content: '删除失败，该用户有未归还图书',
                                    btn: ['确认'],
                                    yes: function (index, layero) {
                                        location.href = '{% url 'manage_user' %}'
                                    }
                                }
                            )
                            }

                        }
                    })
                }
            })
        }
    </script>
{% endblock %}